package org.example.controller;

import org.example.pojo.Article;
import org.example.pojo.Client;
import org.example.pojo.PageBean;
import org.example.pojo.Result;
import org.example.service.ClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/client")
public class ClientController {

    @Autowired
    private ClientService clientService;

    @GetMapping
    public Result<PageBean<Client>> list(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) String state
    ){
        PageBean<Client> pc = clientService.list(pageNum, pageSize, state);
        return Result.success(pc);
    }

    @GetMapping("/detail")
    public Result<Client> detail(Integer id){
        Client c = clientService.getById(id);
        return Result.success(c);
    }

    @PostMapping
    public Result add(@RequestBody @Validated Client client){
        clientService.add(client);
        return Result.success();
    }

    @PutMapping
    public Result update(@RequestBody @Validated Client client){
        clientService.update(client);
        return Result.success();
    }

    @DeleteMapping
    public Result delete(Integer id){
        clientService.delete(id);
        return Result.success();
    }

    @GetMapping("/all")
    public Result<List<Client>> getAllClients(){
        List<Client> all = clientService.getAll();
        return Result.success(all);
    }

    @GetMapping("/byname")
    public Result<List<Client>> getClientByName(String name){
        List<Client> c = clientService.getClientByName(name);
        return Result.success(c);
    }
}
